package com.shoppingmall.photowall;

import java.io.UnsupportedEncodingException;
import java.lang.ref.SoftReference;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

import com.example.shoppingmall.R;
import com.shopping.Model.PhotoWallDetailModel;
import com.shopping.tools.CategoryListTools;
import com.shopping.tools.Constant;
import com.shopping.tools.FileUntils;
import com.shopping.tools.GetUrlMethod;
import com.shoppingmall.Adapter.PhotoWallCommentAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 
 * 照片墙详情 Activity
 *
 */
public class PhotoWallDetail extends Activity implements OnClickListener {
	private String detailID = "";
	private int i = 1;
	private TextView txt_detailpraise, txt_mycomment, txt_commentnumber;
	private ImageView img_back, img_peoplephoto, img_detailphoto;
	private ArrayList<PhotoWallDetailModel> detailList = new ArrayList<PhotoWallDetailModel>();
	private ArrayList<HashMap<String, String>> commentlist = new ArrayList<HashMap<String, String>>();
	private DeatilThread detailThread;
	private String str_detailreq, str_commentreq;
	private ListView listview_comment;
	private ProgressDialog progressDialog;
	PhotoWallCommentAdapter mPhotoWallCommentAdapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 启动activity时不自动弹出软键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.photowall_detail);
		init();
		detailThread = new DeatilThread();
		detailThread.start();// 开启DeatilThread线程

	}

	private void init() {
		// TODO Auto-generated method stub
		detailID = getIntent().getStringExtra("detailID");// 得到照片id
		progressDialog = ProgressDialog.show(PhotoWallDetail.this, "",
				"正在加载中。。。", true, true);
		listview_comment = (ListView) findViewById(R.id.listview_comment);
		txt_commentnumber = (TextView) findViewById(R.id.txt_commentnumber);
		txt_commentnumber.setOnClickListener(this);
		txt_detailpraise = (TextView) findViewById(R.id.txt_detailpraise);
		txt_mycomment = (TextView) findViewById(R.id.txt_mycomment);
		img_peoplephoto = (ImageView) findViewById(R.id.img_peoplephoto);
		img_detailphoto = (ImageView) findViewById(R.id.img_detailphoto);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setOnClickListener(this);
	}

	private class DeatilThread extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			str_detailreq = "{\"id\":" + detailID + ",\"userId\":1}";// 照片墙详情参数
			str_commentreq = "{\"photoId\":" + detailID + "}";// 评论参数
			String urlPath, commentPath;
			try {
				urlPath = GetUrlMethod.getURLString(Constant.photoWallDetailurl
						+ URLEncoder.encode(str_detailreq, "utf-8"));
				commentPath = GetUrlMethod
						.getURLString(Constant.photoCommenturl
								+ URLEncoder.encode(str_commentreq, "utf-8"));
				CategoryListTools.getCategoryListTools()
						.getPhotoWallDetailModel(urlPath, detailList);// 对照片墙详情数据进行解析
				CategoryListTools.json_PhotoWallComment(commentPath,// 对评论数据进行解析
						commentlist);
				String photoThumbnail_url = detailList.get(0)
						.getPhotoThumbnail();// 缩略图图片的路径
				String photoOrigin_url = detailList.get(0).getPhotoOrigin();// 原图片的路径
				downImage(img_peoplephoto, i, photoThumbnail_url);// 缩略图异步加载
				downImage(img_detailphoto, i, photoOrigin_url);// 原图片异步加载

			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Message msg = new Message();
			msg.what = 0;
			myhandler.sendMessage(msg);// 向myhandler发送消息
		}
	};

	@SuppressLint("HandlerLeak")
	private Handler myhandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				txt_detailpraise.setText(detailList.get(0).getPraiseCount());// 详情赞数
				txt_mycomment.setText(detailList.get(0).getSummary());// 评论内容
				txt_commentnumber.setText(commentlist.size() + "");// 评论条数
				mPhotoWallCommentAdapter = new PhotoWallCommentAdapter(
						PhotoWallDetail.this, commentlist);
				listview_comment.setAdapter(mPhotoWallCommentAdapter);
				progressDialog.dismiss();
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img_back:// 点击返回
			this.finish();
			break;
		}
	}

	/**
	 * 键：下载链接 值：图片
	 */
	HashMap<String, SoftReference<Drawable>> imageMap = new HashMap<String, SoftReference<Drawable>>();

	/*
	 * downImage 判断hashmap中有无该图片 有：直接用 没有：下载
	 */
	public void downImage(ImageView view, int positon, String url) {
		// 判断有无图片

		Drawable drawable;
		if ((imageMap.containsKey(url))
				&& ((drawable = imageMap.get(url).get()) != null)) {
			// 有
			view.setImageDrawable(drawable);
		} else {
			// 设置默认图片
			// view.setImageResource(R.drawable.a5);
			// 开启异步加载
			DownImageTask down = new DownImageTask(view, positon);
			down.execute(url);// 启动

		}

	}

	/**
	 * 
	 * Params 传入的参数 url Progress Result 反回值类型
	 * 
	 */
	class DownImageTask extends AsyncTask<String, Integer, Drawable> {
		ImageView view;
		int positon;// 当前item下标
		String url;

		public DownImageTask(ImageView view, int positon) {
			this.view = view;
			this.positon = positon;
		}

		@Override
		protected Drawable doInBackground(String... params) {
			// 执行耗时操作的--下载图片
			url = params[0];
			return FileUntils.getFileUntils().getImageDownload(url);
		}

		@Override
		protected void onPostExecute(Drawable result) {
			// 耗时操作结束需要执行--加载图片
			imageMap.put(url, new SoftReference<Drawable>(result));
			view.setImageDrawable(result);

		}

	}

}
